set more off, perm

* set directory
cd "/Users/cmann/Dropbox/Resistbot/Wisconsin 2019/"

* Automatically generate new log file
local c_date = c(current_date)
local c_time = c(current_time)
local c_time_date = "`c_date'"+"_"+"`c_time'"
local time_str = subinstr("`c_time_date'", ":", "_", .)
local time_str = subinstr("`time_str'", " ", "_", .)
di "`time_str'"


log using "Resistbot 2019 WI_Analysis_`time_str'.txt", text replace


*********************************************
* Resistbot - 2019
* GOTV Experiment - Wisconsin
*********************************************


*~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Created by: C Mann
* Created on: 2019_10_09
* Changed by: C Mann
* Changed on: 2019_10_15
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/* Change log
2019_10_15 -- Added estout wrapper for tables
*/


*--------------------------------	
* Settings
*--------------------------------	
set scheme s1color	

graph set window fontface "Optima"	

* Expand available matrix size
set matsize 800

* Set results buffer (lines retained)
set scrollbufsize 150000

* Stata version
version

* Install estout package for tables
net install estout, replace


cd "/Users/cmann/Dropbox/Resistbot/Wisconsin 2019/"

/*--------------------------------------	
* Build Dataset for Vote History Match
*--------------------------------------	
* Original data and randomization from Jason at Resistbot
* Vote history from Catalist

* import control group data
import delimited "_Source Data/wisconsin-control.txt", clear 
	* create assignment variable
	gen assignment = 0
* save file
save "Resistbot_WI_SupCt_2019.dta", replace

* import treatment group data
import delimited "_Source Data/wisconsin-treatment.txt", clear 
	* create assignment variable
	gen assignment = 1
* Merge
append using "Resistbot_WI_SupCt_2019.dta", force
tab assignment
* save file
saveold "Resistbot_WI_SupCt_2019.dta", replace


*--------------------------------------	
* Import Vote History
*--------------------------------------	

import delimited "_Source Data/ADHOC_2770_APPENDED.txt", clear
	drop firstname lastname address city state zip5 zip zip9 birth_mo birth_day birth_yr
merge 1:1 pdid using "Resistbot_WI_SupCt_2019.dta", gen(_merge_vh) keep(matched using)

*--------------------------------------	
* Remove PII and format
*--------------------------------------	
keep line_number e2019gvm zip5 fieldtotalfaxessent fieldtotalcallsmade fieldtotallettersmailed fieldtotalemailssent fieldtotaleditorialssubmitted _merge_vh assignment

saveold "Resistbot_WI_SupCt_2019_anon.dta", replace
*/

use "Resistbot_WI_SupCt_2019_anon.dta", clear

*--------------------------------------	
* Outcome Variable
*--------------------------------------	

gen s19 = e2019gvm !=""
tab s19 e2019gvm, mi

*--------------------------------------	
* Balance Check
*--------------------------------------	

* Geography
gen zip3 = substr(zip5, 1, 3)
	replace zip3 = "999" if zip3 == "" | zip3 < "530" | zip3 > "549"
	tab zip3, gen(zip3_)

* Matching by Catalist
gen match = _merge_vh==3

	
* Resistbot Activity
foreach var in faxessent callsmade lettersmailed emailssent editorialssubmitted {
	gen `var' = fieldtotal`var'
	replace `var' = 0 if fieldtotal`var' == .
	}

* Variable Labels
label var assignment "Experimental Condition"
label var faxessent "# of faxes sent"
label var callsmade "# of calls made"
label var lettersmailed  "# of letters mailed"
label var emailssent  "# of emails sent"
label var editorialssubmitted  "# of editorials submitted"
label var match "Matched by Catalist"
label var zip3_19 "Missing zip code" 



	
* Balance test (all records)
eststo clear
eststo: logit assignment faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_18 match
	estadd scalar LR = e(chi2)
	estadd scalar df = e(df_m)
	estadd scalar p_value = e(p)
	esttab using "/Users/cmann/Dropbox/Resistbot/_Paper/Tables/Table S2 - Balance Check - Regression - All Records.rtf", ///
	label wide b(3) se(3) stats(N LR df p_value) replace nogaps modelwidth(8) varwidth(22)   ///
		 title({\b Supplemental Table 2:} Balance Check: Multinomial Logistic Regression of Random Assignment on Observable Covariates [All Records])   ///
		 nonumbers mtitles("Random Assignment")   

eststo clear
estpost tabstat faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_19 match, by(assignment) statistics(mean) columns(statistics) listwise
	esttab using "/Users/cmann/Dropbox/Resistbot/_Paper/Tables/Table S1 - Balance Table - All Records.rtf", ///
		replace label main(mean) nostar unstack nomtitle nonumber nonotes ///
		title({\b Supplemental Table 1:} Balance Check: Mean Values of Observable Covariates within Random Assignment Groups [All Records])  	 
	 
* Balance test (matched records)
eststo clear
eststo: logit assignment faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_18 if match == 1
	estadd scalar LR= e(chi2)
	estadd scalar df= e(df_m)
	estadd scalar p_value= e(p)
	esttab using "/Users/cmann/Dropbox/Resistbot/_Paper/Tables/Table S4 - Balance Check - Regression - Matched Records.rtf", ///
	label wide b(3) se(3) stats(N LR df p_value) replace nogaps modelwidth(8) varwidth(22)  ///
		 title({\b Supplemental Table 4:} Balance Check: Multinomial Logistic Regression of Random Assignment on Observable Covariates [Matched Records])   ///
		 nonumbers mtitles("Random Assignment")   

eststo clear
estpost tabstat faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_19 if match == 1, by(assignment) ///
	statistics(mean) columns(statistics) listwise
esttab using "/Users/cmann/Dropbox/Resistbot/_Paper/Tables/Table S3 - Balance Table - Matched Records.rtf", ///
		replace label main(mean) nostar unstack nomtitle nonumber nonotes ///
		title({\b Supplemental Table 3} Balance Check: Mean Values of Observable Covariates within Random Assignment Groups [Matched Records])  	 
	
eststo clear


*--------------------------------------	
* Analysis
*--------------------------------------	

* All records
eststo clear

eststo: reg s19 assignment
eststo: reg s19 assignment faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_18

* Matched records
eststo: reg s19 assignment if match == 1
eststo: reg s19 assignment faxessent callsmade lettersmailed emailssent editorialssubmitted zip3_1-zip3_18 if match == 1

esttab using "/Users/cmann/Dropbox/Resistbot/_Paper/Tables/Table 1 - Turnout Effect.rtf", replace label numbers ///
	cells("b(fmt(3) star)" se(par fmt(3))) stats(N) modelwidth(8) varwidth(22) ///
	title({\b Table 1:} Treatment Effects on Turnout in Wisconsin 2019 Election Using Logistic Regression)       ///
  	mtitles("All Records - No covariates" "All Records - With covariates" "Matched Records - No covariates" "Matched Records - With covariates"   )   ///
	addnote("Notes: Missing zip code is the omitted category.") 

eststo clear

log close
